<script>
	import {uploadAppGps} from './util/api.js'
	import {getTodayTime} from './common/getTimeqj.js'
	export default {
		data(){
			return {
				localtionTimer:null,
				userGpsList:[],
				locationInfo:{
						userId:'',
						lng:'', //精度108
						lat:'',  //维度 35
						createTime:'',
						uuid:'',
						model:'',  //设备型号
						version:'', //系统版本号
						manufacturer:'',  //设备制造商
						satellites:'36', //星数
						gpsStatus:'GPS状态正常'
					} //gps状态GPS状态正常   GPS关闭，建议开启GPS,提高定位质量
				 
			}
		},
		onLaunch: function() {
			// #ifdef APP-PLUS
			//添加定时器调用 解决第一次获取不到clientid
			var t1 = setInterval(()=>{  
			    var info = plus.push.getClientInfo(); 
			    var cid = info.clientid;  
			    if(cid!="null"){  
					uni.setStorageSync('pushId', cid);
			        clearInterval(t1);     
			    }                     
			},1000);
			//监听系统通知栏消息点击事件
			plus.push.addEventListener('click', function(msg) {
				uni.$emit('refush')
			},false);
			//监听透传消息事件
			plus.push.addEventListener('receive', function(msg) {
				uni.$emit('refush')
			},false);
			// this.wakeLock();
			// #endif
			//初始化上传坐标定时器
			this.initTimer();
			// //#ifdef APP-PLUS
			// 	 var info = plus.push.getClientInfo();
			// 	console.log( JSON.stringify( info ) );
			// 	 /* 5+  push 消息推送 ps:使用:H5+的方式监听，实现推送*/  
			// 	plus.push.addEventListener("click", function(msg) {  
			// 				console.log("click:"+JSON.stringify(msg));  
			// 				console.log(msg.payload);  
			// 				console.log(JSON.stringify(msg));  
			// 				//这里可以写跳转业务代码
			// 			}, false);  
			// 				// 监听在线消息事件    
			// 			plus.push.addEventListener("receive", function(msg) {  
			// 				// plus.ui.alert(2);  
			// 					//这里可以写跳转业务代码
			// 				console.log("recevice:"+JSON.stringify(msg))  
			// 	   }, false); 
			// //#endif  	
		},
		onShow: function() {
			var data = uni.getSystemInfoSync()
			var clienId = this.getClientId()
		},
		methods:{
			//初始化时间
			initTimer(){
				uni.$on('startLocationTimer',()=>{
					if(!this.localtionTimer){
						this.localtionTimer=setInterval(()=>{
							console.log("开始定位了")
							this.uploadGps()
						},5000)
					}
				})
				uni.$on('killLocationTimer',()=>{
					console.log("定位关闭了")
					if(this.localtionTimer){
						clearInterval(this.localtionTimer)
						var parmeLotion = uni.getStorageSync('gpsInfo')
						this.localtionTimer=null
						if(parmeLotion){
							this.uploadLoctionGps(parmeLotion.data)
							uni.removeStorageSync("gpsInfo")
						}else{
							console.log("定位关闭了--不上传坐标")
						}
					}
					
				})
			},
			//首先判断gps 是否打卡
			uploadGps(){
				var that = this;
				uni.getLocation({
					type: 'gcj02',
					success(res) {
						that.locationInfo.lng = res.longitude;
						that.locationInfo.lat = res.latitude;
						that.locationInfo.gpsStatus = 'GPS状态正常';
						that.loaddingGps();
					},
					fail(res) {
						that.locationInfo.gpsStatus = 'GPS关闭，建议开启GPS,提高定位质量';
						that.loaddingGps();
						console.log('fail定位失败=' + JSON.stringify(res));
						that.$toast.showToast('手机定位开关未打开，请开启定位开关后再定位')
					},
					complete(res) {
						// console.log('complete定位完成');
					}
				})
			},
			loaddingGps(){
				this.locationInfo.userId = uni.getStorageSync('userInfor').userId;
				this.locationInfo.createTime = getTodayTime();
				this.locationInfo.uuid = this.getClientId();  //uni.getSystemInfoSync().deviceId;
				this.locationInfo.model = uni.getSystemInfoSync().model; //设备型号
				this.locationInfo.version = uni.getSystemInfoSync().version; //系统版本号
				this.locationInfo.manufacturer = uni.getSystemInfoSync().brand; //设备制造商
				let gpsInfo=uni.getStorageSync("gpsInfo");
				if(gpsInfo){
					let lastUpdateTime = gpsInfo.lastUpdateTime;
					let curTime = new Date().getTime();
					console.log(gpsInfo)
					gpsInfo.data.push(this.locationInfo);
					if((curTime-lastUpdateTime)/1000>=30){
						//调取上传接口
						console.log(gpsInfo.data)
						console.error("调用接口")
						this.uploadLoctionGps(gpsInfo.data)
						gpsInfo=null;
					}
					
				}else{
					gpsInfo={
						lastUpdateTime:new Date().getTime(),
						data:[this.locationInfo]
					}
					console.log("保存数据:",gpsInfo)
				}
				uni.setStorageSync("gpsInfo",gpsInfo);
				
			},
			uploadLoctionGps(data){
				var option = {
					userGpsList:JSON.stringify(data)
				}
				console.log(data)
				uploadAppGps(option).then((res)=>{
					if(res.code == 200){
						
					}else{
						
					}
				}).catch((err)=>{
					console.log(err)
				})
			},
			getClientId() {
				// #ifdef APP-PLUS
				const client = plus.push.getClientInfo();
				return client.clientid
				// #endif
			},
			wakeLock() {
				var main = plus.android.runtimeMainActivity();
				var Context = plus.android.importClass("android.content.Context");
				var PowerManager = plus.android.importClass("android.os.PowerManager");
				var pm = main.getSystemService(Context.POWER_SERVICE);
				g_wakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ANY_NAME");
				g_wakelock.acquire();
			},
				
		},
		onHide: function() {
		},
		onUnload() {
			
		}
	}
</script>

<style lang="scss">
	@import "./uview-ui/index.scss";
	/*每个页面公共css */
	.radios .uni-radio-wrapper .uni-radio-input{
		width: 12px !important;
		height: 12px !important;
	}
	.bg{
		padding: 5rpx;
		margin-right: 10rpx;
		font-size: 12px;
	}
	.bg1{
		background-color: #DAF9F6;
		color: #4596FD;
	}
	.bg2{
		background-color: #F6EFCE;
		color: #E5B849;
	}
	.bg3{
		background-color: #F8E6E6;
		color: #FF5039;
	}
	.bg4{
		background-color: #E0F2D6;
		color: #43CE65;
	}
	.center{
		justify-content: center;
		align-items: center;
	}
	.mr20{
		margin-right: 20rpx;
	}
	.mr10{
		margin-right: 10rpx;
	}
	.mb30{
		margin-bottom: 30rpx;
	}
	.mb50{
		margin-bottom: 50rpx;
	}
	.mb20{
		margin-bottom: 20rpx;
	}
	.mt20{
		margin-top: 20rpx;
	}
	.c_45{
		color: #454545;
	}
	.f12{
		font-size: 24rpx;
	}
	.f14{
		font-size: 28rpx;
	}
	.f18{
		font-size: 36rpx;
	}
	.f17{
		font-size: 34rpx;
	}
	.f16{
		font-size: 32rpx;
	}
	.color_r{
		color: red;
	}
	.color_ca{
		color: #CACACA;
	}
	.color_93{
		color: #939393;
	}
	.color_b{
		color: #86A6ED;
	}
	.color_7b{
		color:#7B7B7C;
	}
	.color_76{
		color: #767676;
	}
	.color_45{
		color: $qj-color;
	}
	.color_f{
		color: #fff;
	}
	.boxShow{
		box-shadow: 0px 0px 10px #DFE2ED;
	}
	.flex{
		display: flex;
	}
	.flex-row{
		flex-direction: row;
	}
	.flex-between{
		justify-content: space-between;
	}
	.flex-around{
		justify-content: space-around;
	}
	.align-center{
		align-items: center;
	}
	.justify_center{
		justify-content:center;
	}
	.flex-g{
		flex-grow: 1;
	}
	.fwight{
		font-weight: bold;
	}
	.f_hides{
		overflow:hidden;
		text-overflow:ellipsis;
		white-space:nowrap
	}
	.f_hide2{
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden;
	}
	.lines{
		width: 6rpx;
		height: 30rpx;
		background: #4188F2;
		margin-right: 20rpx;
	}
	.picker_select{
		// width:calc(100% - 140rpx);
		height: 30px;
		border: 1px solid #ced4da;
		box-sizing: border-box;
		border-radius: 5px;
		position: relative;
		overflow: hidden;
		.uni-input{
			line-height: 35px;
			line-height: 30px;
			border-radius: 5px;
			color:#585656;
			padding: 1px 5px;
			font-size: 14px;
			overflow: hidden;
			text-overflow:ellipsis;
			white-space: nowrap;
		}
		.drop-down{
			width:14px;
			height:7px;
			position: absolute;
			top:0;
			bottom:0;
			margin:auto;
			right: 5px;
			/* #ifdef APP-NVUE */
			top: 10px;
			/* #endif */
		}
	}
	.tab{
		width: 100%;
		background-color: #4188F2;
		position: fixed;
		z-index: 2;
		top: 0;
		.tabbar{
			width:100%;
			height:88rpx;
			color: #FFFFFF;
			padding: 35rpx;
			font-size: 32rpx;
			.back{
				width: 24px;
				height: 21px;
			}
			.rightSide{
				.maps{
					width: 20px;
					height: 21px;
					margin-right: 20rpx;
				}
				.slect{
					width: 42rpx;
					height: 42rpx;
				}
			}
		}
	}
</style>
